import plugin from '../../lib/plugins/plugin.js';
import { takeScreenshot } from '../../lib/common/takeScreenshot.js';

export class FengYunZan extends plugin {
  constructor() {
    super({
      name: '赞赏',
      dsc: '赞赏截图',
      event: 'message',
      priority: 5000,
      rule: [
        { reg: '^#我要赞赏$', fnc: 'zanScreenshot' }
      ]
    });

    // 赞赏页地址列表
    this.targetUrls = [
      'http://gj.fengyunyun.icu/IMG_20251015_121559.png', 
      'http://gj.fengyunyun.icu/IMG_20251014_021710.jpg'        
    ];
  }

  async zanScreenshot() {
    await this.e.reply('风云这就去拿饭碗，请稍候~');

    const screenshotPromises = this.targetUrls.map((url, index) => {
      return new Promise(async (resolve) => {
        try {
          const imgPath = await takeScreenshot(
            url,
            `zan_${Date.now()}_${index}`,
            { width: 1920, height: 1080, type: 'jpeg', quality: 80, fullpage: true, waitUntil: 'networkidle2' }
          );
          resolve(segment.image(imgPath));
        } catch (err) {
          resolve(segment.text(`[链接 ${index + 1}]\n截图失败，请检查链接~`));
        }
      });
    });

    const screenshotResults = await Promise.all(screenshotPromises);

    const nodes = [
      { user_id: Bot.uin, nickname: '风云赞赏', message: '制作不易，感谢支持！' },
      ...screenshotResults.map((result, index) => ({
        user_id: Bot.uin,
        nickname: '风云赞赏',
        message: result
      }))
    ];

    const forwardMsg = await Bot.makeForwardMsg(nodes);
    await this.e.reply(forwardMsg);

    return true;
  }
}
